/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;

import Processing.ChessController;
import Processing.GameController;
import Processing.Mark;
import Processing.SaveManager;
import Processing.Util;
import com.sun.perseus.j2d.Point;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;


public class MainForm extends Canvas implements CommandListener
{
    private MyChessMidlet m_midlet;

    private Image ImgHT = null;
    //private Image ImgTemp = null;
    private String title = "Trò chơi cờ";
    private GameController boardProccessing = null;
    private Vector ImgList = new Vector();
    private Vector movingList = new Vector();
    private Hashtable[] movableListResult;
    //private Thread _thrChayCo = null;
    private int xChosenPosition;
    private int yChosenPosition;
    private int pawnChosenPosition = -1;
    private int playingType = 2;
    private int chosenInterface = 0;
    private int[] stepDistance = { 5, 2 };
    private int[] stepTime = { 40, 20 };
    private MakeAMove test = null;
    //private Thread thrTest = null;

    private Command cmdNew;
    private Command cmdUndo;
    private Command cmdLoad;
    private Command cmdSave;
    private Command cmdChangUI;
    private Command cmdExit;

    public MainForm()
    {
    }

    public MainForm(MyChessMidlet midlet)
    {
        m_midlet = midlet;

        cmdNew = new Command("New game", Command.ITEM, 0);
        cmdUndo = new Command("Undo", Command.ITEM, 0);
        cmdLoad = new Command("Load game", Command.ITEM, 0);
        cmdSave = new Command("Save game", Command.ITEM, 0);
        cmdChangUI = new Command("Change UI", Command.ITEM, 0);
        cmdExit = new Command("Exit game", Command.ITEM, 0);

        this.addCommand(cmdNew);
        this.addCommand(cmdUndo);
        this.addCommand(cmdLoad);
        this.addCommand(cmdSave);
        this.addCommand(cmdChangUI);
        this.addCommand(cmdExit);

        this.setCommandListener(this);
        if(m_midlet.option.ChessType == 0)
        {
            title = "Trò chơi cờ Vua";
            boardProccessing = new ChessController();
        }        
        boardProccessing.InitController(1);
        if(m_midlet.option.BlackPlayerType == 1)
        {
            if(m_midlet.option.WhitePlayerType == 1)
            {
                //playingType = 0;
                playingType = 1;
                pawnChosenPosition = -1;
            }
            else
            {
                playingType = 1;
                pawnChosenPosition = -1;
            }
        }
        else
        {
            if(m_midlet.option.WhitePlayerType == 1)
            {
                playingType = 3;
                int[] psNuocCo = boardProccessing.CompMove();
                boardProccessing.PlaceAMove(psNuocCo[0], psNuocCo[1]);
            }
            else
            {
                playingType = 2;
                pawnChosenPosition = -1;
            }
        }

        for (int csGD = 0; csGD < 2; ++csGD )
        {
            Image[] chessInstance = new Image[15];
            for (int i = 0; i < 15; ++i)
            {
                chessInstance[i] = GetPawnInstance(i, 2 * csGD);
            }            

            ImgList.addElement(chessInstance);
        }

        xChosenPosition = boardProccessing.GetSquareNumber()/2;
        yChosenPosition = boardProccessing.GetSquareNumber()/2;
        movingList.removeAllElements();

        ImgHT = Util.CreateBoardWithSelected(boardProccessing.GetPawnList(), (Image[])ImgList.elementAt(boardProccessing.GetChessType() + chosenInterface),
                    boardProccessing.GetBoardWidth(), boardProccessing.GetBoardHeight(), boardProccessing.GetSquareSize(),
                    boardProccessing.GetSquareNumber(), xChosenPosition, yChosenPosition, boardProccessing.GetSelectedImageIndex(), boardProccessing.GetInstance());
    }

    public MainForm(MyChessMidlet midlet, byte[] dl)
    {
        m_midlet = midlet;

        cmdNew = new Command("New game", Command.ITEM, 0);
        cmdUndo = new Command("Undo", Command.ITEM, 0);
        cmdLoad = new Command("Load game", Command.ITEM, 0);
        cmdSave = new Command("Save game", Command.ITEM, 0);
        cmdChangUI = new Command("Change UI", Command.ITEM, 0);
        cmdExit = new Command("Exit game", Command.ITEM, 0);

        this.addCommand(cmdNew);
        this.addCommand(cmdUndo);
        this.addCommand(cmdLoad);
        this.addCommand(cmdSave);
        this.addCommand(cmdChangUI);
        this.addCommand(cmdExit);

        this.setCommandListener(this);

        if(m_midlet.option.ChessType == 0)
        {
            title = "Trò chơi cờ Vua";
            boardProccessing = new ChessController();
        }       

        if(dl[1] == 1)
        {
            playingType = 1;
        }
        else
        {
            if (dl[2] == 1)
            {
                playingType = 3;
            }
            else
            {
                playingType = 2;
            }
        }

        for (int csGD = 0; csGD < 2; ++csGD )
        {
            Image[] chessInstance = new Image[15];
            for (int i = 0; i < 15; ++i)
            {
                chessInstance[i] = GetPawnInstance(i, 2 * csGD);
            }            

            ImgList.addElement(chessInstance);
        }
        xChosenPosition = dl[3];
        yChosenPosition = dl[4];
        chosenInterface = dl[5];

        byte[] dlbc = new byte[dl.length - 6];
        for(int i=0; i<dlbc.length; ++i)
        {
            dlbc[i] = dl[i+6];
        }
        boardProccessing.LoadState(dlbc);
        movingList.removeAllElements();
        ImgHT = Util.CreateBoardWithSelected(boardProccessing.GetPawnList(), (Image[])ImgList.elementAt(boardProccessing.GetChessType() + chosenInterface),
                    boardProccessing.GetBoardWidth(), boardProccessing.GetBoardHeight(), boardProccessing.GetSquareSize(),
                    boardProccessing.GetSquareNumber(), xChosenPosition, yChosenPosition, boardProccessing.GetSelectedImageIndex(), boardProccessing.GetInstance());
    }

    protected void paint(Graphics g)
    {
        this.setTitle(title);
        g.setColor(70, 70, 70);
        g.fillRect(0, 0, getWidth(), getHeight());

        if(test!= null)
        {
            test.Instance();
        }
        
        if(ImgHT != null)
        {
            g.drawImage(ImgHT, 0, 0, Util.StandardDock);
        }
    }

    public void commandAction(Command command, Displayable d)
    {
        if(command == cmdNew)
        {
            m_midlet.switchDisplayable(null, m_midlet.getFormMHChinh());
            return;
        }
        else if(command == cmdUndo)
        {
            UndoGame();
            GetInstance();
            return;
        }
        else if(command == cmdLoad)
        {
            m_midlet.switchDisplayable(null,
                        m_midlet.newFormLoad());
            
            return;
        }
        else if(command == cmdSave)
        {
            Image tb = null;
            
            if (playingType == -1 || playingType == 0)
            {
                m_midlet.switchDisplayable(new Alert("Lỗi", "Không được phép Save !!!",
                        null, AlertType.ERROR),
                        this);
                return;
            }
            byte[] savedBoard = boardProccessing.ExportCurrentState();
            byte[] desave = new byte[savedBoard.length + 6];
            desave[0] = (byte)boardProccessing.GetChessType();
            desave[1] = (byte)m_midlet.option.BlackPlayerType;
            desave[2] = (byte)m_midlet.option.WhitePlayerType;
            desave[3] = (byte)xChosenPosition;
            desave[4] = (byte)yChosenPosition;
            desave[5] = (byte)chosenInterface;
            for(int i=0; i<savedBoard.length; ++i)
            {
                desave[i+6] = savedBoard[i];
            }
            String saveName = ((desave[0] == 0)?"CoVua[":"CoTuong[") + GetCurrentTime() + "].sav";
            SaveManager.OpenDB();
            if(!SaveManager.Write(saveName, desave))
            {
                try
                {
                    tb = Image.createImage("/lose.gif");
                }
                catch(Exception ex)
                {
                }
                m_midlet.switchDisplayable(new Alert("Lỗi", "Không lưu được !!!",
                                    tb, AlertType.ERROR),
                                    this);
            }
            else
            {
                try
                {
                    tb = Image.createImage("/win.gif");
                }
                catch(Exception ex)
                {
                }
                m_midlet.switchDisplayable(new Alert("OK", "Đã hoàn thành lưu !!!",
                                    tb, AlertType.ALARM),
                                    this);
            }
            SaveManager.CloseDB();
        }
        else if(command == cmdChangUI)
        {
            chosenInterface = (chosenInterface == 0)? 2 : 0;
            GetInstance();
            return;
        }
        else if(command == cmdExit)
        {
            m_midlet.switchDisplayable(null, m_midlet.getList());
            return;
        }
    }

    private String GetCurrentTime()
    {
        String result = "";
        Date d = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        result += c.get(Calendar.DAY_OF_MONTH) + "-" + c.get(Calendar.MONTH) + "-" + c.get(Calendar.YEAR);
        result += "_" + c.get(Calendar.HOUR_OF_DAY) + c.get(Calendar.MINUTE) + c.get(Calendar.SECOND);
        return result;
    }

    private void MessageAlert(String al, Image img)
    {
        m_midlet.switchDisplayable(new Alert("Thông báo", al,
                                img, AlertType.ALARM),
                                this);
    }

    private Image CreateImage(String path)
    {
        Image result = null;
        try
        {
            result = Image.createImage(path);
        }
        catch(Exception ex)
        {
        }
        return result;
    }

    private void UndoGame()
    {
        Image img = CreateImage("/lose.gif");
        if (playingType == 0)
            return;
        if(playingType == 3 && boardProccessing.MaxUndoTurn() <= 1)
        {
            MessageAlert("Het quyen Undo !", img);
            return;
        }
        Hashtable[] undoResult = boardProccessing.UndoAMove();
        if (undoResult == null)
        {
            MessageAlert("Het quyen Undo !", img);
            return;
        }

        int chessType = boardProccessing.GetChessType();
        Image originalBoard = Util.CreateBoard(undoResult[0], GetPawnInstanceList(chessType + chosenInterface),
                boardProccessing.GetBoardWidth(), boardProccessing.GetBoardHeight(), boardProccessing.GetSquareSize(),
                boardProccessing.GetSquareNumber());
        Image startBoard = originalBoard;
        int pawn = ((Integer)undoResult[1].get(new Integer(2))).intValue();
        int startPosition = ((Integer)undoResult[1].get(new Integer(0))).intValue();
        int endPosition = ((Integer)undoResult[1].get(new Integer(1))).intValue();
        Mark src = Util.ConvertCord2Pos(boardProccessing.GetSquareSize(), startPosition, boardProccessing.GetSquareNumber());
        Mark des = Util.ConvertCord2Pos(boardProccessing.GetSquareSize(), endPosition, boardProccessing.GetSquareNumber());
        pawnChosenPosition = -1;
        movingList.removeAllElements();

        if (playingType != 2)
        {
            try
            {
                Thread.sleep(100);
            }
            catch (Exception ex)
            {}
            undoResult = boardProccessing.UndoAMove();
            if (undoResult == null)
            {
                return;
            }
            originalBoard = Util.CreateBoard(undoResult[0], GetPawnInstanceList(chessType + chosenInterface),
                boardProccessing.GetBoardWidth(), boardProccessing.GetBoardHeight(), boardProccessing.GetSquareSize(),
                boardProccessing.GetSquareNumber());
            startBoard = originalBoard;
            pawn = ((Integer)undoResult[1].get(new Integer(2))).intValue();
            startPosition = ((Integer)undoResult[1].get(new Integer(0))).intValue();
            endPosition = ((Integer)undoResult[1].get(new Integer(1))).intValue();
            src = Util.ConvertCord2Pos(boardProccessing.GetSquareSize(), startPosition, boardProccessing.GetSquareNumber());
            des = Util.ConvertCord2Pos(boardProccessing.GetSquareSize(), endPosition, boardProccessing.GetSquareNumber());
        }
    }

    protected void keyPressed(int keyCode)
    {
        if(playingType == 0)
        {
            return;
        }
        int chessType = boardProccessing.GetChessType();
        int winPawn = -1;
        switch(getGameAction(keyCode))
        {
            case FIRE:
            {
                if(!boardProccessing.Win())
                {
                    int chosenPosition = boardProccessing.ConvertXY2VT(xChosenPosition, yChosenPosition);
                    if(pawnChosenPosition == -1 || movingList.indexOf(new Integer(chosenPosition)) == -1)
                    {
                        movingList.removeAllElements();
                        movableListResult = boardProccessing.GetMovableList(chosenPosition);
                        if (movableListResult == null)
                        {
                            pawnChosenPosition = -1;
                            ImgHT = Util.CreateBoardWithSelected(boardProccessing.GetPawnList(), GetPawnInstanceList(chessType + chosenInterface),
                                    boardProccessing.GetBoardWidth(), boardProccessing.GetBoardHeight(),
                                    boardProccessing.GetSquareSize(), boardProccessing.GetSquareNumber(), xChosenPosition,
                                    yChosenPosition, boardProccessing.GetSelectedImageIndex(), boardProccessing.GetInstance());
                            repaint();
                            break;
                        }
                        else
                        {
                            pawnChosenPosition = chosenPosition;
                            ImgHT = Util.CreateMovableListWithSelect(movableListResult, GetPawnInstanceList(chessType + chosenInterface),
                                    boardProccessing.GetNextImageIndex(), boardProccessing.GetInstance(), boardProccessing.GetBoardWidth(),
                                    boardProccessing.GetBoardHeight(), boardProccessing.GetSquareSize(), boardProccessing.GetSquareNumber(),
                                    xChosenPosition, yChosenPosition, boardProccessing.GetSelectedImageIndex());

                            movingList = (Vector)movableListResult[1].get("dsNC");
                            repaint();
                            break;
                        }
                    }
                    else
                    {
                        movingList.removeAllElements();
                        Mark boardChecking = new Mark(boardProccessing.GetBoardWidth(), boardProccessing.GetBoardHeight());
                        Hashtable[] stepResult = boardProccessing.PlaceAMove(pawnChosenPosition, chosenPosition);
                        Image originalBoard = Util.CreateBoard(stepResult[0], GetPawnInstanceList(chessType + chosenInterface),
                            boardChecking.X, boardChecking.Y, boardProccessing.GetSquareSize(), boardProccessing.GetSquareNumber());
                        Image startBoard = originalBoard;
                        int goPawn = ((Integer)stepResult[1].get(new Integer(pawnChosenPosition))).intValue();
                        int endPawn = goPawn;
                        if (((Integer)stepResult[1].get(new Integer(chosenPosition))).intValue() != 0)
                        {
                            stepResult[0].put(new Integer(chosenPosition), stepResult[1].get(new Integer(chosenPosition)));
                            startBoard = Util.CreateBoard(stepResult[0], GetPawnInstanceList(chessType + chosenInterface),
                                    boardChecking.X, boardChecking.Y, boardProccessing.GetSquareSize(), boardProccessing.GetSquareNumber());
                        }
                        if(stepResult.length == 3)
                        {
                            endPawn = ((Integer)stepResult[2].get(new Integer(chosenPosition))).intValue();
                        }

                        Mark src = Util.ConvertCord2Pos(boardProccessing.GetSquareSize(), pawnChosenPosition, boardProccessing.GetSquareNumber());
                        Mark des = Util.ConvertCord2Pos(boardProccessing.GetSquareSize(), chosenPosition, boardProccessing.GetSquareNumber());
                        GetInstance();
                        
                        winPawn = boardProccessing.isKing(((Integer)stepResult[1].get(new Integer(chosenPosition))).intValue());
                        Image tb = null;
                        if(winPawn == 1)
                        {
                            try
                            {
                                tb = Image.createImage("/win.gif");
                            }
                            catch(Exception ex)
                            {
                            }
                            m_midlet.switchDisplayable(new Alert("Chúc mừng", "Quân đen đã thắng !!!",
                                    tb, AlertType.INFO),
                                    this);
                        }
                        else if(winPawn == 0)
                        {
                            try
                            {
                                tb = Image.createImage("/win.gif");
                            }
                            catch(Exception ex)
                            {
                            }
                            m_midlet.switchDisplayable(new Alert("Chúc mừng", "Quân trắng đã thắng !!!",
                                    tb, AlertType.INFO),
                                    this);
                        }

                        if (playingType != 2)
                        {
                            int[] psNuocCo = boardProccessing.CompMove();
                            stepResult = boardProccessing.PlaceAMove(psNuocCo[0], psNuocCo[1]);
                            GetInstance();
                            winPawn = boardProccessing.isKing(((Integer)stepResult[1].get(new Integer(chosenPosition))).intValue());
                            if(winPawn == 1)
                            {
                                try
                                {
                                    tb = Image.createImage("/win.gif");
                                }
                                catch(Exception ex)
                                {
                                }
                                m_midlet.switchDisplayable(new Alert("Chúc mừng", "Quân đen đã thắng !!!",
                                        tb, AlertType.INFO),
                                        this);
                            }
                            else if(winPawn == 0)
                            {
                                try
                                {
                                    tb = Image.createImage("/win.gif");
                                }
                                catch(Exception ex)
                                {
                                }
                                m_midlet.switchDisplayable(new Alert("Chúc mừng", "Quân trắng đã thắng !!!",
                                        tb, AlertType.INFO),
                                        this);
                            }
                        }
                    }
                }
                break;
            }
            case UP:
            {
                yChosenPosition--;
                if(yChosenPosition < 0)
                {
                    yChosenPosition = 0;
                }
                GetInstance();
                break;
            }
            case DOWN:
            {
                yChosenPosition++;
                if(yChosenPosition > boardProccessing.GetCollumnSquareNum())
                {
                    yChosenPosition = boardProccessing.GetCollumnSquareNum();
                }
                GetInstance();
                break;
            }
            case LEFT:
            {
                xChosenPosition--;
                if(xChosenPosition < 0)
                {
                    xChosenPosition = 0;
                }
                GetInstance();
                break;
            }
            case RIGHT:
            {
                xChosenPosition++;
                if(xChosenPosition > boardProccessing.GetRowSquareNum())
                {
                    xChosenPosition = boardProccessing.GetRowSquareNum();
                }
                GetInstance();
                break;
            }
        }
    }

    public void keyRepeated(int keyCode)
    {
        if(playingType == 0)
        {
            return;
        }
        switch(getGameAction(keyCode))
        {
            case FIRE:
            {
                break;
            }
            case UP:
            {
                yChosenPosition--;
                if(yChosenPosition < 0)
                {
                    yChosenPosition = 0;
                }
                GetInstance();
                break;
            }
            case DOWN:
            {
                yChosenPosition++;
                if(yChosenPosition > boardProccessing.GetCollumnSquareNum())
                {
                    yChosenPosition = boardProccessing.GetCollumnSquareNum();
                }
                GetInstance();
                break;
            }
            case LEFT:
            {
                xChosenPosition--;
                if(xChosenPosition < 0)
                {
                    xChosenPosition = 0;
                }
                GetInstance();
                break;
            }
            case RIGHT:
            {
                xChosenPosition++;
                if(xChosenPosition > boardProccessing.GetRowSquareNum())
                {
                    xChosenPosition = boardProccessing.GetRowSquareNum();
                }
                GetInstance();
                break;
            }
        }
    }

    private void GetInstance()
    {
        int loaiCo = boardProccessing.GetChessType();
        if(movingList.size() > 0)
        {
            ImgHT = Util.CreateMovableListWithSelect(movableListResult, GetPawnInstanceList(loaiCo + chosenInterface),
                boardProccessing.GetNextImageIndex(), boardProccessing.GetInstance(), boardProccessing.GetBoardWidth(),
                boardProccessing.GetBoardHeight(), boardProccessing.GetSquareSize(), boardProccessing.GetSquareNumber(),
                xChosenPosition, yChosenPosition, boardProccessing.GetSelectedImageIndex());
        }
        else
        {
            ImgHT = Util.CreateBoardWithSelected(boardProccessing.GetPawnList(), (Image[])ImgList.elementAt(boardProccessing.GetChessType() + chosenInterface),
                boardProccessing.GetBoardWidth(), boardProccessing.GetBoardHeight(), boardProccessing.GetSquareSize(),
                boardProccessing.GetSquareNumber(), xChosenPosition, yChosenPosition, boardProccessing.GetSelectedImageIndex(), boardProccessing.GetInstance());
        }
        repaint();
    }

    private Image GetPawnInstance(int qc, int loaiCo)
    {
        String strKQ = "/";
        switch(loaiCo)
        {
            case 0:
                strKQ += "Chess_";
                break;
            case 2:
                strKQ += "Chess2_";
                break;
        }
        strKQ += String.valueOf(qc) + ".png";
        try
        {
            return Image.createImage(strKQ);
        }
        catch(Exception ex)
        {
            
        }
        return null;
    }

    private void TwoCompPlaying()
    {
        int pawnWin = -1;
        int chessType = boardProccessing.GetChessType();
        Mark boardSize = new Mark(boardProccessing.GetBoardWidth(), boardProccessing.GetBoardHeight());
        while (!boardProccessing.Win())
        {
            try
            {
                Thread.sleep(100);
            }
            catch(Exception ex)
            {

            }

            int[] moveList = boardProccessing.CompMove();
            Hashtable[] moveResult = boardProccessing.PlaceAMove(moveList[0], moveList[1]);
            pawnWin = boardProccessing.isKing(((Integer)moveResult[1].get(new Integer(moveList[1]))).intValue());
            Image bcOri = Util.CreateBoard(moveResult[0], GetPawnInstanceList(chessType + chosenInterface),
                    boardSize.X, boardSize.Y, boardProccessing.GetSquareSize(), boardProccessing.GetSquareNumber());
            Image bcStart = bcOri;
            int movePawn = ((Integer)moveResult[1].get(new Integer(moveList[0]))).intValue();
            int destPawn = movePawn;
            if (((Integer)moveResult[1].get(new Integer(moveList[1]))).intValue() != 0)
            {
                moveResult[0].put(new Integer(moveList[1]), moveResult[1].get(new Integer(moveList[1])));
                bcStart = Util.CreateBoard(moveResult[0], GetPawnInstanceList(chessType + chosenInterface),
                        boardSize.X, boardSize.Y, boardProccessing.GetSquareSize(), boardProccessing.GetSquareNumber());
            }
            if(moveResult.length == 3)
            {
                destPawn = ((Integer)moveResult[2].get(new Integer(moveList[1]))).intValue();
            }

            Mark src = Util.ConvertCord2Pos(boardProccessing.GetSquareSize(), moveList[0], boardProccessing.GetSquareNumber());
            Mark des = Util.ConvertCord2Pos(boardProccessing.GetSquareSize(), moveList[1], boardProccessing.GetSquareNumber());
            MakeAMove(src, des, bcStart, bcOri, GetPawnInstance(movePawn, chessType + chosenInterface),
                    GetPawnInstance(destPawn, chessType + chosenInterface), stepDistance[chessType], stepTime[chessType]);
            repaint();
        }
    }

    private Image[] GetPawnInstanceList(int loaiUI)
    {
        return (Image[])ImgList.elementAt(loaiUI);
    }

    private void MakeAMove(Mark src, Mark des, Image bkDau, Image bkCuoi, Image th, Image thSau, int kcStep, int thoigian)
    {
        Graphics gtemp;        
        ImgHT = Image.createImage(bkCuoi.getWidth(), bkCuoi.getHeight());
        gtemp = ImgHT.getGraphics();
        gtemp.drawImage(bkCuoi, 0, 0, Util.StandardDock);
        gtemp.drawImage(thSau, src.X, src.Y, Util.StandardDock);
        //repaint();
    }

    class MakeAMove implements Runnable
    {
        private Mark src;
        private Mark des;
        private Image bkStart;
        private Image bkDest;
        private Image th;
        private Image thDest;
        private int kcStep;
        private int time;

        public MakeAMove(Mark src, Mark des, Image bkDau, Image bkCuoi,
                Image th, Image thSau, int kcStep, int thoigian)
        {
            this.src = src;
            this.des = des;
            this.bkStart = bkDau;
            this.bkDest = bkCuoi;
            this.th = th;
            this.thDest = thSau;
            this.kcStep = kcStep;
            this.time = thoigian;
        }

        public void Instance()
        {
            if(!src.equals(des))
            {
                ImgHT = Image.createImage(bkStart.getWidth(), bkStart.getHeight());
                Graphics gtemp = ImgHT.getGraphics();
                gtemp.drawImage(bkStart, 0, 0, Util.StandardDock);
                gtemp.drawImage(th, src.X, src.Y, Util.StandardDock);
            }
            else
            {
                ImgHT = Image.createImage(bkDest.getWidth(), bkDest.getHeight());
                Graphics gtemp = ImgHT.getGraphics();
                gtemp.drawImage(bkDest, 0, 0, Util.StandardDock);
                gtemp.drawImage(thDest, src.X, src.Y, Util.StandardDock);
            }
        }

        public void run()
        {
            while(!src.equals(des))
            {
                try
                {
                    Thread.sleep(time);
                }
                catch(Exception ex)
                {

                }
                if (src.X != des.X)
                {
                    src.X = (src.X > des.X) ? (src.X - kcStep) : (src.X + kcStep);
                }
                if (src.Y != des.Y)
                {
                    src.Y = (src.Y > des.Y) ? (src.Y - kcStep) : (src.Y + kcStep);
                }
                repaint();
            }
        }
    }
}
